package vista;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.nio.charset.CodingErrorAction;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

import controlador.Controlador;
import model.MaterialView;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class AltaPrendaSinTemporada extends javax.swing.JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Controlador controlador;
	private JDesktopPane jDesktopPane1;
	private JLabel jLabel2;
	private JLabel jLabel3;
	private JLabel jLabel4;
	private JScrollPane jScrollPane1;
	private JList<String> ListaMateriales;
	private DefaultListModel<String> ModeloListaMateriales;
	private JButton AgregarMaterial;
	private JLabel jLabel5;
	private JScrollPane jScrollPane2;
	private JButton cancelar;
	private JButton aceptar;
	private DefaultListModel<String> ModeloListaMaterialesAgregados;
	private JList<String> ListaMaterialesAgregados;
	private JTextField Cantidad;
	private JLabel jLabel7;
	private JTextField MaterialSeleccionado;
	private JLabel jLabel6;
	private JTextField codigoText;
	private JTextField nombreText;
	private JLabel jLabel1;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				AltaPrendaSinTemporada inst = new AltaPrendaSinTemporada();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public AltaPrendaSinTemporada() {
		super();
		controlador = Controlador.getInstancia();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				jDesktopPane1 = new JDesktopPane();
				getContentPane().add(jDesktopPane1, BorderLayout.CENTER);
				{
					jLabel1 = new JLabel();
					jDesktopPane1.add(jLabel1);
					jLabel1.setText("Alta Prenda Sin Temporada");
					jLabel1.setBounds(163, 12, 213, 18);
				}
				{
					jLabel2 = new JLabel();
					jDesktopPane1.add(jLabel2);
					jLabel2.setText("Nombre:");
					jLabel2.setBounds(12, 57, 78, 16);
				}
				{
					jLabel3 = new JLabel();
					jDesktopPane1.add(jLabel3);
					jLabel3.setText("Codigo:");
					jLabel3.setBounds(314, 57, 76, 16);
				}
				{
					nombreText = new JTextField();
					jDesktopPane1.add(nombreText);
					nombreText.setBounds(84, 56, 189, 19);
				}
				{
					codigoText = new JTextField();
					jDesktopPane1.add(codigoText);
					codigoText.setBounds(372, 56, 104, 21);
				}
				{
					jLabel4 = new JLabel();
					jDesktopPane1.add(jLabel4);
					jLabel4.setText("Materiales:");
					jLabel4.setBounds(12, 138, 70, 13);
				}
				{
					jScrollPane1 = new JScrollPane();
					jDesktopPane1.add(jScrollPane1);
					jScrollPane1.setBounds(84, 113, 144, 105);
					{
					/*	ModeloListaMateriales = new DefaultListModel<String>();
						ModeloListaMateriales.addElement(new String("uno"));
						ModeloListaMateriales.addElement(new String("dos"));*/
						
						ModeloListaMateriales = controlador.getMaterialesView();
						
						ListaMateriales = new JList<String>();
						jScrollPane1.setViewportView(ListaMateriales);
						ListaMateriales.setModel(ModeloListaMateriales);
						
						ListaMateriales.addMouseListener(new MouseListener() {
							
							@Override
							public void mouseReleased(MouseEvent e) {
								// TODO Auto-generated method stub
								
							}
							
							@Override
							public void mousePressed(MouseEvent e) {
								// TODO Auto-generated method stub
								
							}
							
							@Override
							public void mouseExited(MouseEvent e) {
								// TODO Auto-generated method stub
								
							}
							
							@Override
							public void mouseEntered(MouseEvent e) {
								// TODO Auto-generated method stub
								
							}
							
							@Override
							public void mouseClicked(MouseEvent e) {
								
								MaterialSeleccionado.setText(ListaMateriales.getSelectedValue().toString());
								Cantidad.setText("");
								Cantidad.requestFocus();
								
							}
						});
					}
				}
				{
					AgregarMaterial = new JButton();
					jDesktopPane1.add(AgregarMaterial);
					AgregarMaterial.setText("Agregar");
					AgregarMaterial.setBounds(253, 263, 84, 23);
					
					AgregarMaterial.addActionListener(new ActionListener(){

						@Override
						public void actionPerformed(ActionEvent arg0) {
							
							ModeloListaMaterialesAgregados.addElement(new String(ListaMateriales.getSelectedValue()+" - "+Cantidad.getText().toString()));
							//ListaMateriales.remove(ListaMateriales.getSelectedIndex());
							Cantidad.setText("");
							MaterialSeleccionado.setText("");
						}
						
					});
					
				}
				{
					jLabel5 = new JLabel();
					jDesktopPane1.add(jLabel5);
					jLabel5.setText("Lista");
					jLabel5.setBounds(12, 116, 41, 16);
				}
				{
					jLabel6 = new JLabel();
					jDesktopPane1.add(jLabel6);
					jLabel6.setText("Material:");
					jLabel6.setBounds(12, 233, 67, 16);
				}
				{
					MaterialSeleccionado = new JTextField();
					MaterialSeleccionado.setEnabled(false);
					jDesktopPane1.add(MaterialSeleccionado);
					MaterialSeleccionado.setBounds(84, 230, 144, 19);
				}
				{
					jLabel7 = new JLabel();
					jDesktopPane1.add(jLabel7);
					jLabel7.setText("Cantidad:");
					jLabel7.setBounds(12, 270, 60, 14);
				}
				{
					Cantidad = new JTextField();
					jDesktopPane1.add(Cantidad);
					Cantidad.setBounds(84, 266, 144, 20);
				}
				{
					jScrollPane2 = new JScrollPane();
					jDesktopPane1.add(jScrollPane2);
					jScrollPane2.setBounds(273, 113, 227, 69);
					{
						ModeloListaMaterialesAgregados = new DefaultListModel<String>();
						//ModeloListaMaterialesAgregados.addElement(new String( "Item One"));
						ListaMaterialesAgregados = new JList<String>();
						jScrollPane2.setViewportView(ListaMaterialesAgregados);
						ListaMaterialesAgregados.setModel(ModeloListaMaterialesAgregados);
					}
				}
				{
					aceptar = new JButton();
					jDesktopPane1.add(aceptar);
					aceptar.setText("Aceptar");
					aceptar.setBounds(285, 194, 91, 23);
					
					aceptar.addActionListener( new ActionListener(){

						@Override
						public void actionPerformed(ActionEvent arg0) {
							
							int codigoPrenda =Integer.parseInt(codigoText.getText());
							String nombre = nombreText.getText().toString();
							
							controlador.altaPrendaSinTemporada(codigoPrenda, nombre, 0);
							
							for(int i=0;i<ListaMaterialesAgregados.getModel().getSize();i++){
								
								String material =ListaMaterialesAgregados.getModel().getElementAt(i);
								String[] partes= material.split("-");
								//partes.split()
								int codigoMaterial = Integer.parseInt(partes[0]);	
								int cantidad = Integer.parseInt(partes[2].trim());
								
								controlador.agregarMaterialAPrenda(codigoPrenda, codigoMaterial, cantidad);
								
								JOptionPane.showMessageDialog(null, "Prenda guardada correctamente.");
								cerrarVentana();
								
							}
							
							
							//cerrarVentana();
							
						}
						
					});
				}
				{
					cancelar = new JButton();
					jDesktopPane1.add(cancelar);
					cancelar.setText("Cancelar");
					cancelar.setBounds(408, 194, 93, 23);
					
					cancelar.addActionListener( new ActionListener(){

						@Override
						public void actionPerformed(ActionEvent arg0) {
							cerrarVentana();
							
						}
						
					});
				}
			}
			pack();
			this.setSize(541, 336);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void cerrarVentana(){
		this.dispose();
	}

}
